<?php

namespace app\common\model;

use think\Model;

class TotalTime extends Model
{
    protected $name = 'total_time';

    /**
     * 修改今日余额
     * @param $type 类型
     * @param $money    金额
     * @return DividendTime
     */
    public static function upTodayMoney($type,$money){
        $time = strtotime(date('Y-m-d',time()));
        //查询今日余额
        $newData = self::where('time',$time)->where('type',$type)->find();
        if(!empty($newData)){
            //已存在，执行修改
            $data = [
                'total_num' => bcadd($money,$newData['total_num'],8)
            ];
            self::where('id',$newData['id'])->update($data);

            return true;
        }else{
            //不存在，执行添加
            $data = [
                'type' => $type,
                'total_num' => $money,
                'time'  =>  $time
            ];
            self::insert($data);

            return true;
        }
    }

    /**
     * 获取昨天
     * @param $type
     * @return TotalTime|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function yesterday($type){
        $time = strtotime(date("Y-m-d",strtotime("-1 day")));
        $info = self::where('time',$time)->where('type',$type)->find();
        return $info;
    }


}